<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="720px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改代理' : '添加代理'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="代理账号:" prop="oemCode">
            <el-input
              clearable
              :maxlength="11"
              :disabled="isUpdate"
              v-model="form.oemCode"
              placeholder="请输入代理账号"
            />
          </el-form-item>
          <el-form-item label="代理类型:" prop="agencyId">
            <el-select
              clearable
              class="ele-block"
              v-model="form.agencyId"
              placeholder="请选择代理类型"
            >
              <el-option v-for="(i,j) in typeList" :key="j" :label="i.agencyName" :value="i.id" />
            </el-select>
          </el-form-item>

          <el-form-item label="域名:" prop="proxyAddress">
            <el-input
              clearable
              :maxlength="40"
              v-model="form.proxyAddress"
              placeholder="请输入OEM域名"
            />
          </el-form-item>
          <!--<el-form-item label="平台详情" prop="platformDetails">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.platformDetails"
              placeholder="请输入平台名称"
            />
          </el-form-item>-->
           <!--prop="counts"-->
          <!--<el-form-item label="余额" class="delInput">
            <el-input
              clearable
              type="number"
              v-model.number="form.counts"
              placeholder="请输入余额"
            />
          </el-form-item>-->
          <el-form-item label="公司名称:" prop="corporateName">
            <el-input
              clearable
              :maxlength="30"
              v-model="form.corporateName"
              placeholder="请输入公司名称"
            />
          </el-form-item>
          </el-col>
          <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
            <!--修改密码时，隐藏盖子字段-->
            <el-form-item label="密码:" prop="oemPassword" v-if="!data">
              <el-input
                clearable
                show-password
                :maxlength="20"
                v-model="form.oemPassword"
                placeholder="请输入密码"
              />
            </el-form-item>
            <el-form-item label="联系人" prop="linkman">
              <el-input
                clearable
                :maxlength="20"
                v-model="form.linkman"
                placeholder="请输入联系人"
              />
            </el-form-item>
            <el-form-item label="联系电话" prop="phone">
              <el-input
                :maxlength="11"
                clearable
                v-model="form.phone"
                placeholder="请输入联系电话"
              />
            </el-form-item>

            <el-form-item label="到期时间" prop="expirationTime">
              <el-date-picker
                type="date"
                class="ele-fluid"
                v-model="form.expirationTime"
                value-format="yyyy-MM-dd"
                placeholder="请选择到期时间"
              />
              <!--<div>当前代理到期时间为{{form.expirationTime}}</div>-->
            </el-form-item>

          </el-col>
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import { emailReg, phoneReg } from 'ele-admin';
  import RoleSelect from './role-select.vue';
  import ActivityEdit from './activity-edit.vue';
  import { addUser, updateUser, checkExistence } from '@/api/oem';
  import {JSEncrypt} from "jsencrypt";

  export default {
    components: { RoleSelect,ActivityEdit},
    props: {
      // 代理类型
      typeList: Array,
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        oemType: 1,
        id:'',
        oemCode: '',
        oemPassword: '',
        counts: '',
        platformDetails: '',
        corporateName:'',
        agencyId:'',
        linkman:'',
        phone:'',
        proxyAddress:'',
        expirationTime:''
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          agencyId: [
            {
              required: true,
              message: '请输入代理类型',
              trigger: 'blur'
            }
          ],
          oemCode: [
            {
              required: true,
              message: '请输入11位数的代理手机号',
              trigger: 'blur'
            },
            {
              pattern: /^1[3456789]\d{9}$/,
              message: '请输入正确的代理手机号',
              trigger: 'change'
            }
            /*,
            {
              pattern:/[a-zA-Z0-9]{6,11}$/,
              message: '请输入6-11位数的代理账号(仅字母和数字)'
            }*/
          ],
          oemPassword: [
            {
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              pattern: /^[\S]{6,18}$/,
              message: '密码必须为6-18位非空白字符',
              trigger: 'blur'
            }
          ],
          platformDetails: [
            {
              required: true,
              message: '请输入平台详情',
              trigger: 'blur'
            }
          ],
          proxyAddress: [
            {
              required: true,
              message: '请输入域名地址',
              trigger: 'blur'
            },
            {
              pattern: /(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/,
              message: '请输入正确的域名地址',
            },
          ],
          phone: [
            {
              required: true,
              message: '请输入联系人电话',
              trigger: 'blur'
            },
            {
              pattern:/^1[3,4,5,6,7,8,9][0-9]{9}$/,
              message: '请输入正确的电话号码',
            }
          ],

          expirationTime: [
            {
              required: true,
              message: '请选择到期时间',
              trigger: 'blur'
            }
          ],
          linkman: [
            {
              required: true,
              message: '请输入联系人',
              trigger: 'blur'
            }
          ],
          counts: [
            {
              required: true,
              message: '请输入余额',
              trigger: 'blur'
            }
          ],
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form,
          };
          const saveOrUpdate = this.isUpdate ? updateUser : addUser;
          // if (!this.isUpdate) {
            let encryptor = new JSEncrypt()
            let pk = this.$store.state.signature.pk
            encryptor.setPublicKey(pk)
            data.oemPassword = encryptor.encrypt(data.oemPassword)
          // }
          saveOrUpdate(data)
            .then((msg) => {
              this.loading = false;
              let hint = this.isUpdate ? '修改代理成功！' : '添加代理成功！'
              this.$message.success(hint);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            console.log("this.data",this.data)
            this.$util.assignObject(this.form, {
              ...this.data,
              password: null,
              sex: this.data.sex + ''
            });
            this.isUpdate = true;
            console.log("this.form111111111111111",this.form)

          } else {
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>

<style scoped lang="less">
  /*隐藏input（type=number)右边的小箭头*/
  /deep/.delInput input::-webkit-outer-spin-button,
  /deep/.delInput input::-webkit-inner-spin-button {
    -webkit-appearance: none!important;
  }
  /deep/.delInput input[type="number"]{
    -moz-appearance: textfield;
  }
</style>
